<?php

namespace app\model;

use app\utils\Md5Service;
use \think\facade\Db;
use think\facade\Log;

class DriverModel
{
    public static function attendance_date($param, $nowMonth)
    {
        $where[] = [
            'partner_id', '=', $param['partner_id'],
        ];
        $where[] = [
            'driver_id', '=', $param['driver_id'],
        ];
        $where[] = [
            'day', '>', $nowMonth['firstDay'],
        ];
        $where[] = [
            'day', '<', $nowMonth['lastDay'],
        ];
//        if (isset($param['name'])){
//            $where[]=['name','like','%'.$param['name'].'%'];
//        }
        return Db::table('xl_driver_attendance')->where("status", "=", 1)
            ->where($where)
            ->select()->toArray();
    }

    public static function lists($param)
    {
        $dataPartition = PartnerUser::where('id',request()->param('uid'))->value('source');
//        $userIds = getScopeView($param['uid']);
//        if ($userIds != 0) {
//            $where[] = ['r.uid', 'in', $userIds];
//        }
        $where[] = [
            'r.partner_id', '=', $param['partner_id'],
        ];
        $where[] = [
            'r.uid', 'like',$dataPartition."%",
        ];
        if (isset($param['name']) && !empty($param['name'])) {
            $where[] = ['r.name', 'like', '%' . $param['name'] . '%'];
        }
        if (isset($param['mobile']) && !empty($param['mobile'])) {
            $where[] = ['r.mobile', 'like', '%' . $param['mobile'] . '%'];
        }  
        if (isset($param['company_name']) && !empty($param['company_name'])) {
            $where[] = ['c.name', 'like', '%' . $param['company_name'] . '%'];
        }               
        if ($param['is_export'] == 1) {
            $param['limit'] = Db::table('xl_driver')->alias('r')->where($where)->count();
        }
        $list = Db::table('xl_driver')->alias('r')->where("r.status", "=", 1)
            ->join('xl_company c', 'c.id = r.company_id', 'left')
            ->field('c.name as company_name,r.*')
            ->where($where)
            ->limit(($param['page']-1) * $param['limit'], $param['limit'])
            ->select()->toArray();

        $count = Db::table('xl_driver')->alias('r')->where("r.status", "=", 1)
            ->join('xl_company c', 'c.id = r.company_id', 'left')
            ->where($where)
            ->limit(($param['page']-1) * $param['limit'], $param['limit'])
            ->count();
        return ['list' => $list, 'count' => $count];
    }


    public static function line_data($param)
    {
        $where[] = [
            'uid', 'like', $param['source'].'%'
        ];

        $list = Db::table('xl_driver')->where("status", "=", 1)
            ->where($where)
            ->field('id,name')
            ->select()->toArray();

        foreach ($list as $k => $v) {
            $condition[] = ['driver_ids', 'find in set', $v['id']];

            $count = Db::table('xl_parnter_line')->where('status', '=', 1)->where($condition)->count();
            if ($count > 0) {
                //已被使用
                $list[$k]['type'] = 1;
            } else {
                //未被使用
                $list[$k]['type'] = 2;

            }
        }
        return $list;
    }

    /**
     * 获取所有仓库类型
     * @return void
     */
    public static function GetAllWarehouseType()
    {
        return Db::table('xl_warehouse_type')->where("status", "=", 1)
            ->select();

    }

    /**
     * 获取所有业务类型
     * @return void
     */
    public static function GetAllBussineType()
    {
        return Db::table('xl_business_type')->where("status", "=", 1)
            ->select();

    }

    /**
     * 新增司机信息
     * @return void
     */
    public static function add($data)
    {
        return Db::table('xl_driver')->insertGetId($data);

    }

    /**
     * 编辑司机信息
     * @return void
     */
    public static function edit($data, $id)
    {
        return Db::table('xl_driver')->where('id', '=', $id)->update($data);

    }


    /**
     * 删除司机信息
     * @return void
     */
    public static function del($id)
    {
        return Db::table('xl_driver')->where('id', '=', $id)->update(['status' => 3]);

    }

    /**
     * 获取司机信息
     * @return void
     */
    public static function info($id)
    {
        return Db::table('xl_driver')->where('id', '=', $id)->field('id,name')->find();

    }


}
